
from django.shortcuts import render, HttpResponseRedirect, redirect
from django.core.files.uploadedfile import UploadedFile
import cv2
import os
from predictor import predictor
import json
from django.http import HttpResponse
import requests
from  recact.utils import *

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    # "/static/"
]


# 视频截取和结果判断，两个功能通过不同的method区分
def video_recording(req):
    # 录制视频的操作
        # print("getttt")
    return render(req, 'index.html', {'result':""})


# 提交保存视频，并返回运行结果的操作
def content_update(req):
    if req.method == "POST":
        video_obj = req.FILES.get("file_obj")
        fname=STATICFILES_DIRS[0] + "/" + "test.mkv"
        with open(fname, 'wb') as f:
            for c in video_obj.chunks():
                f.write(c)
        # 返回结果
        pred_result = predictor(fname)
        baike_content, related_link_list, img_path, related_link_abstract, related_link_title = get_related_content(pred_result)
        json_data = {"result": pred_result[2:], 'baike_content': baike_content,
                     'relate_link': related_link_list,
                     "img_path": img_path,
                     "relate_link_abstract":related_link_abstract,
                     "relate_link_title": related_link_title}
        return HttpResponse(json.dumps(json_data), content_type='application/json', charset='utf-8')